<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\Exception;

class Supplier extends BaseController
{
    //查询此公司下的所有供应商
    public function Get_SupplierInfo()
    {
        $userId = $this->request->auth['data']->user->user_id;
        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $current_page = Request::param('current_page');
        $page_size = Request::param('page_size');

        try {
            $data = Db::name('supplier')
                ->where('supplier_company_id', $companyId)
                ->page($current_page, $page_size)
                ->select();

            $total = Db::name('supplier')
                ->where('supplier_company_id', $companyId)
                ->count();

            return json([
                'code' => 200,
                'msg' => '查询成功',
                'data' => [
                    'total' => $total,
                    'data' => $data,
                ],
            ]);

        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => '查询失败']);
        }

    }

    //新增供应商,要考虑供应商名称是否重复，要开启事务考虑回滚问题
    public function Insert_SupplierInfo()
    {
        $userId = $this->request->auth['data']->user->user_id;
        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $data=Request::param('data');

        $data['supplier_company_id'] = $companyId;
        $data['supplier_switch'] = 1;

        try {
            Db::startTrans();
            // 验证供应商名称是否重复
            if (Db::name('supplier')->where('supplier_name', $data['supplier_name'])->where('supplier_company_id', $companyId)->count() > 0) {
                return json(['code' => 500, 'msg' => '供应商名称重复']);
            }
            //直接添加供应商
            Db::name('supplier')->insert($data);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '新增供应商成功'
            ]);

        } catch (Exception $e) {
            Db::rollback();
            return json(['code' => 500, 'msg' => '新增供应商失败'.$e->getMessage()]);
        }




    }

    //修改供应商信息,要开启事务考虑回滚问题
    public function Update_SupplierInfo()
    {
        $supplier_id = Request::param('id');
        $data=Request::param('data');
        try {
            Db::startTrans();
//            // 验证供应商名称是否重复
//            if (Db::name('supplier')->where('supplier_name', $data['supplier_name'])->where('supplier_company_id', $data['supplier_company_id'])->where('supplier_id', '<>', $supplier_id)->count() > 0) {
//                return json(['code' => 500, 'msg' => '供应商名称重复']);
//            }
            //直接更新供应商
            Db::name('supplier')->where('supplier_id', $supplier_id)->update($data);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '修改供应商信息成功'
            ]);

        } catch (Exception $e) {
            Db::rollback();
            return json(['code' => 500, 'msg' => '修改供应商信息失败'.$e->getMessage()]);
        }

    }

    //删除供应商信息,要开启事务考虑回滚问题
    public function Delete_SupplierInfo()
    {
        $supplier_id = Request::param('id');
        try {
            Db::startTrans();
            //直接删除供应商
            Db::name('supplier')->where('supplier_id', $supplier_id)->delete($supplier_id);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '删除供应商信息成功'
            ]);

        } catch (Exception $e) {
            Db::rollback();
            if($e->getCode()==10501)
            {
                return json(['code' => 500, 'msg' => '该供应商下已存在单据，无法删除']);
            }

            return json(['code' => 500, 'msg' => '删除供应商信息失败'.$e->getMessage()]);
        }

    }


}